package com.whfc.fuum.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.fuum.dao.SysDictMapper;
import com.whfc.fuum.dto.SysDictDTO;
import com.whfc.fuum.entity.SysDict;
import com.whfc.fuum.param.SysDictDataAddParam;
import com.whfc.fuum.param.SysDictDataEditParam;
import com.whfc.fuum.param.SysDictTypeAddParam;
import com.whfc.fuum.param.SysDictTypeEditParam;
import com.whfc.fuum.redis.SysDictRedisDao;
import com.whfc.fuum.service.SysDictService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/25 10:43
 */
@DubboService(interfaceClass = SysDictService.class, version = "1.0.0")
public class SysDictServiceImpl implements SysDictService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysDictMapper dictMapper;

    @Autowired
    private SysDictRedisDao sysDictRedisDao;


    @Override
    public PageData<SysDictDTO> typeList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysDictDTO> list = dictMapper.selectTypeList();
        PageHelper.clearPage();

        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void addType(SysDictTypeAddParam request) {
        // 不能重复添加
        SysDictDTO sysDictDTO = dictMapper.selectByNameAndPid(request.getName(), 0);
        if (sysDictDTO != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典类型已存在，不能重复添加！");
        }
        SysDict record = new SysDict();
        record.setCode(request.getCode());
        record.setName(request.getName());
        record.setPid(0);
        dictMapper.insertSelective(record);
    }

    @Override
    public void editType(SysDictTypeEditParam request) {
        SysDictDTO sysDictDTO = dictMapper.selectByNameAndPid(request.getName(), 0);
        if (sysDictDTO != null && !sysDictDTO.getId().equals(request.getId())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典类型名称已存在！");
        }
        SysDict record = new SysDict();
        BeanUtils.copyProperties(request, record);
        dictMapper.updateByPrimaryKeySelective(record);

        //更新缓存
        sysDictRedisDao.remove(record.getId());
    }

    @Override
    public void delType(Integer id) {
        // 先查询该字典类型是否有数据
        List<SysDictDTO> list = dictMapper.selectByPid(id);
        if (!list.isEmpty()) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典类型下有数据，不能删除！");
        }

        dictMapper.deleteByPrimaryKey(id);

        //更新缓存
        sysDictRedisDao.remove(id);
    }


    @Override
    public PageData<SysDictDTO> listDictData(Integer sysDictTypeId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysDictDTO> list = dictMapper.selectByPid(sysDictTypeId);
        PageHelper.clearPage();

        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public void addDictData(SysDictDataAddParam request) {
        Integer pid = request.getSysDictTypeId();
        // 查询该字典类型存不存在
        SysDict sysDict = dictMapper.selectByPrimaryKey(request.getSysDictTypeId());
        if (sysDict == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典类型不存在！");
        }
        SysDictDTO sysDictDTO = dictMapper.selectByNameAndPid(request.getName(), pid);
        if (sysDictDTO != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典数据名称已存在！");
        }
        SysDict record = new SysDict();
        record.setCode(request.getCode());
        record.setPid(request.getSysDictTypeId());
        record.setName(request.getName());
        dictMapper.insertSelective(record);
    }

    @Override
    public void editDictData(SysDictDataEditParam request) {
        Integer id = request.getId();
        SysDict sysDict = dictMapper.selectByPrimaryKey(id);
        Integer pid = sysDict.getPid();
        SysDictDTO sysDictDTO = dictMapper.selectByNameAndPid(request.getName(), pid);
        if (sysDictDTO != null && !sysDictDTO.getId().equals(request.getId())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典数据名称已存在！");
        }

        SysDict record = new SysDict();
        BeanUtils.copyProperties(request, record);
        dictMapper.updateByPrimaryKeySelective(record);

        //更新缓存
        sysDictRedisDao.remove(id);
    }

    @Override
    public void delDictData(Integer id) {
        dictMapper.deleteByPrimaryKey(id);

        //更新缓存
        sysDictRedisDao.remove(id);
    }


    @Override
    public SysDictDTO getDictById(Integer id) {
        SysDictDTO dictDTO = null;
        if (sysDictRedisDao.exists(id)) {
            dictDTO = sysDictRedisDao.getDict(id);

        } else {
            SysDict dict = dictMapper.selectByPrimaryKey(id);
            if (dict != null) {
                dictDTO = new SysDictDTO();
                BeanUtils.copyProperties(dict, dictDTO);
                sysDictRedisDao.setDict(id, dictDTO);
            }
        }
        return dictDTO;
    }

    @Override
    public String getDictName(Integer id) {

        String dictName = null;
        SysDictDTO dict = this.getDictById(id);
        if (dict != null) {
            dictName = dict.getName();
        }
        return dictName;
    }

    @Override
    public SysDictDTO getDictDataByTypeCode(String dictTypeCode) {
        SysDictDTO sysDictDTO = dictMapper.selectByCode(dictTypeCode);
        if (sysDictDTO == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "该字典类型不存在");
        }
        List<SysDictDTO> childList = dictMapper.selectByPid(sysDictDTO.getId());
        sysDictDTO.setChildList(childList);
        return sysDictDTO;
    }

    @Override
    public Integer getOrSet(String code, String name) throws BizException {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        //根据父code和名称查找
        SysDictDTO dict = dictMapper.selectByNameAndParentCode(name, code);
        if (dict != null) {
            return dict.getId();
        }
        SysDictDTO parentDict = dictMapper.selectByCode(code);
        if (parentDict == null) {
            return null;
        }
        SysDict sysDict = new SysDict();
        sysDict.setPid(parentDict.getId());
        sysDict.setName(name);
        dictMapper.insertSelective(sysDict);
        return sysDict.getId();
    }
}
